﻿Imports System.Data.SqlClient
Public Class FrmNhapBanDo
    Public m_tennhap As String
    Private Sub NapDonViHuyen()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select * from TENHUYEN", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        ComboBoxHuyen.DisplayMember = "T_huyen"
        ComboBoxHuyen.ValueMember = "maH"
        ComboBoxHuyen.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapKho()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select * from DMKho", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        ComboBoxKho.DisplayMember = "TenKho"
        ComboBoxKho.ValueMember = "maKhoID"
        ComboBoxKho.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapKe()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select * from DMKe", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        ComboBoxKe.DisplayMember = "TenKe"
        ComboBoxKe.ValueMember = "maKeID"
        ComboBoxKe.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapDMHienTrang()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select * from DMHienTrang", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        ComboBoxHienTrang.DisplayMember = "TenHienTrang"
        ComboBoxHienTrang.ValueMember = "MaHienTrang"
        ComboBoxHienTrang.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapDMTyLe()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select * from DMTyLe", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        ComboBoxTyLe.DisplayMember = "TenTyLe"
        ComboBoxTyLe.ValueMember = "MaTyle"
        ComboBoxTyLe.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapLoaiTaiLieu()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select * from DMTaiLieu", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        ComboBoxLoaiTaiLieu.DisplayMember = "TenLoaiTaiLieu"
        ComboBoxLoaiTaiLieu.ValueMember = "maLoaiTaiLieuID"
        ComboBoxLoaiTaiLieu.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub FrmNhapBanDo_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' to mau cac dong trong datagridlietkebando doi mau khac nhau
        DataGridViewLietKeBanDo.RowsDefaultCellStyle.BackColor = Color.Beige
        DataGridViewLietKeBanDo.AlternatingRowsDefaultCellStyle.BackColor = Color.Azure
        ' doi mau tung dong trong datagridbando 
        DataGridViewBanDo.RowsDefaultCellStyle.BackColor = Color.Beige
        DataGridViewBanDo.AlternatingRowsDefaultCellStyle.BackColor = Color.Azure
        ' đinh ngay hien tai cho ngay nhap
        DateTimeNgayNhap.Text = Today()
        ' nap danh sach huyen

        ' tat chuc nang nut luu
        Me.ButtonLuu.Enabled = False
        '-------------
        NapDonViHuyen()
        NapKho()
        NapKe()
        NapDMHienTrang()
        NapLoaiTaiLieu()
        NapDataGridViewBanDoDiaChinh()
        NapDMTyLe()
        TextBoxMaBD.Text = checkSoHoSo() + 1
    End Sub
    Public Function checkSoHoSo()
        ob_cnn.Open()
        Dim sqlCom As New SqlCommand("select max(masoBD) from BanDoDiaChinh  ", ob_cnn)
        Dim max As Int32 = Convert.ToInt32(sqlCom.ExecuteScalar())
        ob_cnn.Close()
        Return max
    End Function
    Private Sub NhapBanDoDiaChinh()
        Try
            Dim tennhap As String
            tennhap = ModuleTenDangNhap.m_tennhap
            Dim NgayNhap As String
            NgayNhap = String.Format("{0:MM/dd/yyyy}", DateTimeNgayNhap.Value)
            Dim Sql As String = ""
            Sql = " INSERT INTO BanDoDiaChinh (masoBD, NgayNhap,NamThanhLap, MaHuyenId, MaXaId,ToBanDoSo, " &
                "TyLeID,MaKhoId,MaKeId,MaTinhTrangId,MaHienTrangID,NguoiNhap, Ngay, MaLoaiTaiLieuID," &
                " TongThua, TongDienTich, DonViDo, TongToBanDo, DiaDanh )" &
           "  VALUES" &
           " ( '" & TextBoxMaBD.Text & "', " &
            " '" & NgayNhap & "' ," &
           " '" & TextBoxNamThanhLap.Text & "' ," &
            " '" & ComboBoxHuyen.SelectedValue.ToString & "', " &
            " '" & ComboBoxXa.SelectedValue.ToString & "', " &
            " '" & TextBoxToBanDo.Text & "', " &
            " '" & ComboBoxTyLe.SelectedValue.ToString & "', " &
            " '" & ComboBoxKho.SelectedValue.ToString & "', " &
            " '" & ComboBoxKe.SelectedValue.ToString & "', " &
            " '1', " &
            " '" & ComboBoxHienTrang.SelectedValue.ToString & "', " &
            " N'" & tennhap & "', " &
            " " & Format(Now.ToOADate() - 2) & ", " &
            " '" & ComboBoxLoaiTaiLieu.SelectedValue.ToString & "' , " &
            " " & TextBoxTongThua.Text & ", " &
            "  " & TextBoxDienTich.Text & ", " &
            " N'" & TextBoxDonViDo.Text & "', " &
            " '" & TextBoxTongToBanDo.Text & "', " &
            " N'" & TextBoxDiaDanh.Text & "')"
            Dim cmd As New SqlCommand(Sql, ob_cnn)
            ob_cnn.Open()
            cmd.ExecuteNonQuery()
            ob_cnn.Close()
            MsgBox("Thêm thông tin thành công!")
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
    Public Sub NapDuLieuNhapVaodataGird()
        DataGridViewBanDo.Rows.Add(TextBoxToBanDo.Text, TextBoxDiaDanh.Text, TextBoxNamThanhLap.Text, TextBoxDonViDo.Text)
    End Sub
    Private Sub ComboBoxHuyen_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBoxHuyen.SelectedIndexChanged
        ' load ten huyen
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select * from TenXa where mahuyen = '" & ComboBoxHuyen.SelectedValue.ToString & "' ", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        ComboBoxXa.DisplayMember = "TenXa"
        ComboBoxXa.ValueMember = "maxa"
        ComboBoxXa.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapDataGridViewBanDoDiaChinh()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand("select  ToBanDoSo, TongToBanDo, tl.tentyle,  TongDienTich,TongThua, Diadanh  From  BanDoDiaChinh dc, DMTyLe tl  WHERE  MaXaId = '" & ComboBoxXa.SelectedValue.ToString & "' " &
                                " and MaHuyenId = '" & ComboBoxHuyen.SelectedValue.ToString & "' " &
                                " and dc.tyleid=tl.matyle " &
                                " and MaLoaiTaiLieuID = '" & ComboBoxLoaiTaiLieu.SelectedValue.ToString & "' ", ob_cnn) '
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        DataGridViewLietKeBanDo.DataSource = tbl

        ob_cnn.Close()
    End Sub

    Private Sub DataGridViewLietKeBanDo_RowPostPaint(sender As Object, e As DataGridViewRowPostPaintEventArgs) Handles DataGridViewLietKeBanDo.RowPostPaint
        Dim strRowNumber As String = (e.RowIndex + 1).ToString
        While (strRowNumber.Length < DataGridViewLietKeBanDo.RowCount.ToString.Length)
            strRowNumber = "0" & strRowNumber
        End While
        Dim Size As SizeF = e.Graphics.MeasureString(strRowNumber, MyBase.Font)
        If DataGridViewLietKeBanDo.RowHeadersWidth < Size.Width + 20 Then DataGridViewLietKeBanDo.RowHeadersWidth = Size.Width + 20
        Dim b As Brush = SystemBrushes.ControlText
        e.Graphics.DrawString(strRowNumber, MyBase.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y +
                              ((e.RowBounds.Height - Size.Height) / 2))
    End Sub
    Private Sub DataGridViewBanDo_RowPostPaint(sender As Object, e As DataGridViewRowPostPaintEventArgs) Handles DataGridViewBanDo.RowPostPaint
        Dim strRowNumber As String = (e.RowIndex + 1).ToString
        While (strRowNumber.Length < DataGridViewBanDo.RowCount.ToString.Length)
            strRowNumber = "0" & strRowNumber
        End While
        Dim Size As SizeF = e.Graphics.MeasureString(strRowNumber, MyBase.Font)
        If DataGridViewBanDo.RowHeadersWidth < Size.Width + 20 Then DataGridViewBanDo.RowHeadersWidth = Size.Width + 20
        Dim b As Brush = SystemBrushes.ControlText
        e.Graphics.DrawString(strRowNumber, MyBase.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y +
                              ((e.RowBounds.Height - Size.Height) / 2))

    End Sub

    Private Sub ButtonNhap_Click(sender As Object, e As EventArgs) Handles ButtonNhap.Click
        Me.ButtonLuu.Enabled = True


    End Sub
    Public Function checkTobando()
        ob_cnn.Open()
        Dim sqlCom As New SqlCommand("select count(ToBanDoSo) from BanDoDiaChinh WHERE  MaXaId = '" & ComboBoxXa.SelectedValue.ToString & "' " &
                                " and MaHuyenId = '" & ComboBoxHuyen.SelectedValue.ToString & "' " &
                                " and MaLoaiTaiLieuID = '" & ComboBoxLoaiTaiLieu.SelectedValue.ToString & "' ", ob_cnn)
        Dim count As Int32 = Convert.ToString(sqlCom.ExecuteScalar())
        ob_cnn.Close()
        Return count
    End Function

    Private Sub ButtonLuu_Click(sender As Object, e As EventArgs) Handles ButtonLuu.Click
        If TextBoxToBanDo.Text = "" Then
            MessageBox.Show("Số tờ bản đồ không được rỗng")
            Return
        End If
        'If (checkTobando(TextBoxToBanDo.Text) > 0) Then
        '    MsgBox(" Tờ bản đồ đã trùng, bạn kiểm tra lại ")
        '    Return
        'End If

        If TextBoxDiaDanh.Text = "" Then
            MessageBox.Show("Chưa nhập thông tin địa danh tờ bản đồ")
            Return
        End If
        'If TextBoxTyLe.Text < "0" Or TextBoxTyLe.Text = "" Then
        '    MessageBox.Show("Tỷ lệ bản đồ không được âm hoặc bằng không ")
        '    Return
        'End If
        If TextBoxDonViDo.Text = "" Then
            MessageBox.Show("Chưa nhập thông tin đơn vị đo đạc")
            Return
        End If
        If TextBoxNamThanhLap.Text < "0" Or TextBoxNamThanhLap.Text = "" Then
            MessageBox.Show("Năm thành lập bản đồ phải lớn hơn không ")
            Return
        Else
            NhapBanDoDiaChinh()
            NapDuLieuNhapVaodataGird()
            NapDataGridViewBanDoDiaChinh()
            TextBoxMaBD.Text = checkSoHoSo() + 1
            ob_cnn.Close()
        End If

      
    End Sub

    Private Sub ButtonThoat_Click(sender As Object, e As EventArgs) Handles ButtonThoat.Click
        Me.Close()

    End Sub
    Private Sub ComboBoxLoaiTaiLieu_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBoxLoaiTaiLieu.SelectedIndexChanged
        NapDataGridViewBanDoDiaChinh()
    End Sub

   
    Private Sub ComboBoxHienTrang_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBoxHienTrang.SelectedIndexChanged

    End Sub

    Private Sub DataGridViewBanDo_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridViewBanDo.CellContentClick

    End Sub

    Private Sub GroupBox1_Enter(sender As Object, e As EventArgs) Handles GroupBox1.Enter

    End Sub
End Class